function ProjectCenterTrain() {
    var _this = this;
    var page;
    _this.page_size = 10;

    // init
    _this.init = function(_page) {
        page = _page;
        _this.initTable();
        _this.initEvent();
    };

    _this.initTable = function() {
        var list_url = appPath + "/admin/projectCenterTrain/projectList";
        page.init("projectCenterTrainForm", list_url, "pro_tbody", "pro_page", 1, _this.page_size);
        page.goPage(1);
        page.list = function(dataList){
            _this.obj = dataList;
            if(dataList == null){
                return;
            }
            var len = dataList.length;
            var inner = "", item;
            // 组装数据
            for(var i=0; i< len; i++) {
                item = dataList[i];
                // 组装数据
                inner += "<tr>";
                inner += "<td><input type=\"checkbox\" name=\"proBox\" value=\"" + item.id +"\" template=\"";
                if(item.hasOwnProperty('estimateReport')){
                    inner += "template";
                }
                inner += "\"></td>";
                inner += "<td><span class=\"text-orange tooltip\" data-length='30'>"+item.project_name+"</span></td>";
                inner += "<td>"+(TimeUtil.longMsTimeConvertToDateTime(item['project_start_time'])+"<br>"+TimeUtil.longMsTimeConvertToDateTime(item['project_end_time']))+"</td>";
                inner += "<td>"+item.person_count+"</td>";
                inner += "<td>"+Enum.projectType(item.project_type)+"</td>";
                inner += "<td><span class=\"text-blue\">" +Enum.projectStatus(item.project_status)+ "</span></td>";
                inner += "<td>"+item.create_user+"</td>";
                inner += "<td>"+TimeUtil.longMsTimeConvertToDateTime(item.create_time)+"</td>";
                inner += "<td>";
                if(1 == item.project_status && item.permission) {
                    inner += '<a  onclick=\"projectCenterTrain.project_is_publish(\'' + item.id + '\',\'' + item.project_type + '\',\'' + TimeUtil.longMsTimeToDateTime(item.project_start_time) + '\',\'' + TimeUtil.longMsTimeToDateTime(item.project_end_time) + '\',1);\" class=\"a a-info\">发布</a>';
                }

                if(2 == item.project_status && item.permission) {
                    inner += '<a  onclick=\"projectCenterTrain.project_is_publish(\'' + item.id + '\',\'' + item.project_type + '\',\'' + TimeUtil.longMsTimeToDateTime(item.project_start_time) + '\',\'' + TimeUtil.longMsTimeToDateTime(item.project_end_time) + '\',2);\" class=\"a  a-close\">取消发布</a>';
                }

                inner += '<a href="javascript:projectCenterTrain.projectInfo(\''+item.id+'\');" class="a a-view">详情</a> ';

                if(4 != item.project_status && item.permission){
                    inner += '<a href="javascript:projectCenterTrain.modify(\''+item.id + '\',\''+ item.project_type + '\',\''+ item.project_status  +'\');" class="a a-info">修改</a> ';
                }else{
                    inner +=  '<a href="javascript:;"  class="a">修改</a>';
                }
                if((2 == item.project_status || 1 == item.project_status) && item.permission) {
                    inner += '<a href="javascript:projectCenterTrain.del(\'' + item.id + '\');" class="a a-close">删除</a>';
                }else{
                    inner += '<a href="javascript:;" class="a">删除</a>';
                }
                /*if(1 != item.project_status) {
                    inner += '<a href="javascript:projectCenterTrain.datumManage(\'' + item.id + '\');" class="a a-view">资料管理</a>';
                }*/
                if((4 == item.project_status || 3 == item.project_status) && item.permission){
                    inner += '<a href="javascript:projectCenterTrain.kaoqinManager(\''+item.id+'\');" class="a a-info">考勤管理</a> ';
                    inner += '<a href="javascript:projectCenterTrain.startTrain(\''+item.id+'\');" class="a a-info">开展培训</a> ';
                }
                if((5 == item.project_type && 1 != item.project_status) && item.permission){
                    inner += '<a href="javascript:projectCenterTrain.scordManage(\''+item.id+'\');" class="a a-view">成绩管理</a> ';
                    inner += '<a href="javascript:projectCenterTrain.exportPaper(\''+item.id+'\');" class="a a-info">打印试卷</a> ';
                    // 包含主观题
                    if(1 == item.readPaper && (3 == item.project_status || 4 == item.project_status) && item.permission){
                        inner += '<a href="javascript:projectCenterTrain.readPaper(\''+item.id+'\');" class="a a-view">主观题阅卷</a> ';
                    }
                }
                /*if(1 != item.project_status) {
                    inner += '<a href="javascript:projectCenterTrain.exportData(\'' + item.id + '\',\'' + item.project_name + '\',\'' + item.person_count + '\');" class="a a-view">导出</a> ';
                }*/
                inner += "</td>";
                inner += "</tr>";
            }
            return inner;
        }

    };

    // 项目自定义排序
    _this.sort = function(value, obj) {
        $("#sortBy").val(value);
        var order = obj.className
        if(order == 'sort asc'){
            $("#descAsc").val('DESC')
        }else{
            $("#descAsc").val('ASC')
        }
        // 刷新列表
        _this.initTable(page);
    };

    _this.initEvent = function () {
        //搜索
        $("#project_search").click(function () {
            _this.initTable();
        });

        //全部
        $("#project_all").click(function () {
            $("#projectStartTime").val("");
            $("#projectEndTime").val("");
            $("#projectName").val("");
            _this.initTable();
        });

        //项目类型切换
        $('#click_project_type').on("click", "span", function(){
            $('#click_project_type').find("span").removeClass("active");
            $(this).addClass("active");
            bind_click(this);
        });
    };


    bind_click = function(t){
        var value = $(t).attr("data-value");
        $("#projectType").val(value);
        _this.initTable(page);
    };


    // 打印试卷
    _this.exportPaper = function (projectId) {
        var exam_no = '';
        // var winopn = window.open("", "project_paper");
        // //如果ajax响应时间过长，就给个提示
        // winopn.document.write('<style>body{margin:0;padding:0;background: #e8edf0;overflow: hidden;}</style><img src="' + resourcePath + 'static/global/images/import_paper.png" style="width:100%;height:auto;margin-top:9%;"/>');
        var flag = true;
        while (flag){
            var url = appPath + '/admin/exportProjectPaper';
            var param = {"projectId": projectId, "roleId": "-1"};
            var data = ajax(url, param);
            console.log(data);
            var code = data['code'];
            if (code == 10000) {
                var result = data['result'];
                if (result && result != '') {
                    flag = false;
                    exam_no = result;
                    setCookie('exam_no' + projectId, exam_no);
                    // window.setTimeout(function () {
                    //     var url = appPath + "/admin/exportExam?examNo=" + exam_no + "&projectId=" + projectId + "&isCenterTrain=" + $("#isCenterTrain").val();
                    //     winopn.location = url;
                    // }, 2500);
                    window.location.href = appPath +  "/admin/exportExam?examNo=" + exam_no + "&projectId=" + projectId + "&isCenterTrain=" + "1";
                } else {
                    flag = true;
                }
            } else {
                flag = false;
                layer.alert("网络异常", {icon: 3});
            }
        }
    }



    function ajax(url,param){
        var result = '';
        $.ajax({
            url: url,
            async: false,
            type: 'post',
            data: param,
            success: function(data){
                result = data;
            }
        });
        return result;
    }


    // 项目详情
    _this.projectInfo = function (projectId) {
        var url = appPath + "/admin/projectCenterTrain/proManagerInfo?projectId="+projectId+"&type=proManager";
        window.open(url,"_blank");
    };

    // 修改项目
    _this.modify = function(projectId,projectType,projectStatus) {
        location.href = appPath +"/admin/projectCenterTrain/basicInfoModify?projectId="+projectId+"&projectTypeNo="+projectType+"&projectStatus="+projectStatus;
    };

    // 删除项目
    _this.del = function (projectId) {
        layer.confirm("确定删除吗?", {
            icon : 3,
            btn : [ "确认", "取消" ]
        }, function(index) {
            $.ajax({
                url: appPath + '/admin/project/proManagerDelete',
                dataType: 'json',
                async: false,
                type: 'post',
                data: {'projectId': projectId, 'isCenterTrain':1},
                success: function (data) {
                    /*window.location.reload(true);
                     page.goPage()*/
                    layer.close(index);
                    page.refresh_cur_page();
                }
            });
        });
    };


    // 成绩管理
    _this.scordManage = function (projectId) {
        var url = appPath + "/admin/projectCenterTrain/scoreManage/index?projectId="+projectId;
        window.open(url,"_blank");
    };

    // 开展培训
    _this.startTrain = function (projectId) {
        var url = appPath + "/admin/projectCenterTrain/startTrain/index?projectId="+projectId;
        window.open(url,"_blank");
    };

    // 考勤管理
    _this.kaoqinManager = function (projectId) {
        var url = appPath + "/admin/projectCenterTrain/kaoqinManager/index?projectId="+projectId;
        window.open(url,"_blank");
    };

    // 资料管理
    _this.datumManage = function (projectId) {
        layer.msg("此功能待开发！",{icon:7})
    };

    // 导出
    _this.exportData = function (projectId, projectName, personCount) {
        layer.msg("此功能待开发！",{icon:7})
    };

    // 创建项目
    _this.createOfflineProject = function () {
        window.location.href = appPath + "/admin/projectCenterTrain/createCenterTrainProject";
    }



    /*取消或者发布项目*/
    _this.project_is_publish = function (id, projectType, startTime, endTime, createType, projectStatus) {
        var url = "/admin/project/proManagerPublish";
        var param = {
            'id': id,
            'projectType': projectType,
            'startTime': startTime,
            'endTime': endTime,
            'type': createType,
            'isCenterTrain': 1  //0线上项目；1集中培训
        }
        if (createType == 1) {
            layer.confirm("确定要发布该项目吗?", {icon: 3}, function () {
                $.ajax({
                    url: appPath + url,
                    type: "POST",
                    cache: false,
                    async: false,
                    dataType: "json",
                    data: param,
                    success: function (result) {
                        if (result.code == '10000' && result.result == null) {
                            _this.initTable();
                            layer.msg("发布成功", {time: 1000});
                        } else {
                            layer.alert(result.result);
                        }
                    }
                });
            });
        }
        if (createType == 2) {
            layer.confirm("确定要取消发布该项目吗?", {icon: 3}, function () {
                $.ajax({
                    url: appPath + url,
                    type: "POST",
                    cache: false,
                    async: false,
                    dataType: "json",
                    data: param,
                    success: function (result) {
                        if (result.code == '10000') {
                            _this.initTable();
                            layer.msg("取消发布成功", {time: 1000});
                        } else {
                            layer.msg("取消发布失败");
                        }
                    }
                });
            });
        }
        if (createType == 3) {
            $.ajax({
                url: appPath + "/admin/project/proManagerPublish",
                type: "POST",
                cache: false,
                async: false,
                dataType: "json",
                data: {'id': id, 'type': createType, 'projectStatus': projectStatus},
                success: function (result) {

                }
            });
        }
    }

    // 主观题阅卷
    _this.readPaper = function (projectId) {
        // 是否有人 线上正式考试
        $.ajax({
            url: appPath + '/readPaper/projectExamUserCount',
            async: false,
            type: 'post',
            data: {'projectId':projectId, 'examStatus':'2,3'},
            success: function (data) {
                if(data.code != 10000){
                    layer.msg(data.message);
                    return;
                }
                var url = appPath + "/readPaper/index?projectId="+projectId;
                window.open(url,"_blank");
            }
        });
    }

    //弹窗-选择模板
    _this.chooseTemplate = function() {
        var projectIds=[]
        $('#pro_tbody input:checked').each(function(i,e){
            projectIds.push($(this).val());
        });
        if(projectIds.length==0 || projectIds.length > 1){
            layer.msg("请选择一个项目");return;
        }
        //先判断是否可选择模板
        $.ajax({
            url: appPath + '/project/estimateQuestion/changeTemplate',
            async: false,
            type: 'post',
            data: {'projectId': projectIds[0]},
            success: function (data) {
                if (data.code == 10000) {
                    layer.open({
                        type: 2,
                        title: '效果评估模板选择',
                        area: ['70%', '80%'],		//弹出层大小
                        scrollbar: false,				//false隐藏滑动块
                        content: [appPath + '/admin/estimate/choose?projectId=' + projectIds[0] + '&projectType=centerLine', 'yes']
                    });
                }else{
                    layer.msg(data.message);
                }
            }
        });
    };

    // 导出评估结果
    _this.exportReport = function() {
        var projectIds=[]
        $('#pro_tbody input:checked').each(function(i,e){
            projectIds.push($(this).val());
        });
        if(projectIds.length==0 || projectIds.length > 1){
            layer.msg("请选择一个项目");return;
        }
        var obj = $('#pro_tbody input:checked');
        var template = obj.attr("template");
        // 已选择模板， 导出评估结果
        if('' != template){
            window.location.href = appPath + '/admin/projectEstimate/exportReport?projectId=' + projectIds[0]+'&projectType=centerLine';
        }else{
            layer.msg("当前项目未设置评估模板");
        }
    }

    _this.estimateReport = function(projectId, projectStatus) {
        if('3' == projectStatus || '4' == projectStatus){
            var url = appPath + "/admin/projectEstimate/report?projectId="+projectId+'&projectType=centerLine';
            window.open(url,"_blank");
        }else{
            layer.msg('项目尚未开始，暂无评估结果');
        }

    }
}

var projectCenterTrain = new ProjectCenterTrain();